package com.xqboss.web.controller.equipment;

import cn.hutool.core.bean.BeanUtil;
import com.xqboss.apps.dto.PageDto;
import com.xqboss.apps.dto.equipment.EquipmentSmeltTemplateNftQueryDto;
import com.xqboss.apps.dto.equipment.EquipmentSmeltTemplateNftSaveDto;
import com.xqboss.apps.dto.equipment.EquipmentSmeltTemplateSaveDto;
import com.xqboss.apps.enums.nft.NftTypeEnum;
import com.xqboss.apps.service.equipment.EquipmentSmeltTemplateService;
import com.xqboss.apps.vo.equipment.EquipmentSmeltTemplateDetailVo;
import com.xqboss.apps.vo.equipment.EquipmentSmeltTemplateNftVo;
import com.xqboss.apps.vo.equipment.EquipmentSmeltTemplateVo;
import com.xqboss.common.annotation.Log;
import com.xqboss.common.annotation.RepeatSubmit;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.PageList;
import com.xqboss.common.core.domain.R;
import com.xqboss.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author Trent
 * @date 2023/8/16
 */
@Api(tags = "装备-装备熔炼模板")
@RequestMapping("/web/equipment/smelt/template")
@RestController
public class EquipmentSmeltTemplateController extends BaseController {

    @Autowired
    private EquipmentSmeltTemplateService smeltTemplateService;

    @ApiOperation("列表")
    @GetMapping("/list")
    public R<List<EquipmentSmeltTemplateVo>> list() {
        List<EquipmentSmeltTemplateVo> list =  BeanUtil.copyToList(smeltTemplateService.list(), EquipmentSmeltTemplateVo.class);
        return R.ok(list);
    }

    @PreAuthorize("@ss.hasPermi('web:equipment:smelt:template:list')")
    @ApiOperation("分页列表")
    @GetMapping("/page/list")
    public R<List<EquipmentSmeltTemplateVo>> pageList(PageDto params) {
        PageList<EquipmentSmeltTemplateVo> list = smeltTemplateService.pageList(params);
        return R.ok(list.getList(), list.getTotal());
    }

    @PreAuthorize("@ss.hasPermi('web:equipment:smelt:template:detail')")
    @ApiOperation("获取模板详情")
    @GetMapping("/detail/{id}")
    public R<EquipmentSmeltTemplateDetailVo> detail(@PathVariable("id")
                                                       @ApiParam(value = "模板ID", required = true)
                                                       @NotNull(message = "请输入属性模板ID")
                                                       Long id) {
        EquipmentSmeltTemplateDetailVo detail = smeltTemplateService.detail(id);
        return R.ok(detail);
    }

    @PreAuthorize("@ss.hasPermi('web:equipment:smelt:template:nft:list')")
    @ApiOperation("关联藏品分页列表")
    @GetMapping("/nft/page/list")
    public R<List<EquipmentSmeltTemplateNftVo>> nftPageList(EquipmentSmeltTemplateNftQueryDto params) {
        params.setNftType(NftTypeEnum.EQUIPMENT.getValue());
        PageList<EquipmentSmeltTemplateNftVo> pageList = smeltTemplateService.nftPageList(params);
        return R.ok(pageList.getList(), pageList.getTotal());
    }

    @PreAuthorize("@ss.hasPermi('web:equipment:smelt:template:nft:list')")
    @ApiOperation("产出藏品分页列表")
    @GetMapping("/produce/nft/page/list")
    public R<List<EquipmentSmeltTemplateNftVo>> produceNftPageList(PageDto params) {
        EquipmentSmeltTemplateNftQueryDto queryDto = BeanUtil.copyProperties(params, EquipmentSmeltTemplateNftQueryDto.class);
        queryDto.setNftType(NftTypeEnum.NORMAL.getValue());
        PageList<EquipmentSmeltTemplateNftVo> pageList = smeltTemplateService.nftPageList(queryDto);
        return R.ok(pageList.getList(), pageList.getTotal());
    }

    @PreAuthorize("@ss.hasPermi('web:equipment:smelt:template:ntf:save')")
    @ApiOperation("关联藏品保存")
    @PostMapping("/nft/save")
    public R nftSave(@RequestBody @Validated EquipmentSmeltTemplateNftSaveDto params) {
        smeltTemplateService.nftSave(params);
        return R.ok();
    }

    @RepeatSubmit(interval = 1000, justUrl = true)
    @PreAuthorize("@ss.hasPermi('web:equipment:smelt:template:save')")
    @ApiOperation("添加模板")
    @PostMapping("/add")
    public R add(@Validated @RequestBody EquipmentSmeltTemplateSaveDto params) {
        smeltTemplateService.add(params);
        return R.ok();
    }

    @RepeatSubmit(interval = 1000, justUrl = true)
    @PreAuthorize("@ss.hasPermi('web:equipment:smelt:template:save')")
    @ApiOperation("修改模板")
    @PostMapping("/edit")
    public R edit(@Validated @RequestBody EquipmentSmeltTemplateSaveDto params) {
        smeltTemplateService.edit(params);
        return R.ok();
    }


    @PreAuthorize("@ss.hasPermi('web:equipment:smtel:template:delete')")
    @ApiOperation("删除模板")
    @DeleteMapping("/delete/{id}")
    @Log(title = "装备熔炼模板", businessType = BusinessType.DELETE)
    public R delete(@PathVariable("id")
                 @ApiParam(value = "属性模板ID", required = true)
                 @NotNull(message = "请输入属性模板ID")
                 Long id) {
        smeltTemplateService.delete(id);
        return R.ok();
    }
}
